#!/bin/ash -efu
#
# mki-pack-custom
#
# This file is part of mkimage
# Copyright (C) 2007-2009  Alexey Gladkov <gladkov.alexey@gmail.com>
#
# This file is covered by the GNU General Public License,
# which should be included with mkimage as the file COPYING.
#

. "${0%/*}"/mki-sh-functions

verbose "has started executing."

handler="${MKI_HANDLER:?handler required}"

pkgs="${PACKAGES_REQUIRED_PACK_CUSTOM:-}"

outname="${MKI_OUTNAME:-outname}"
outname="${outname##*/}"

imgsubdir="${MKI_IMAGESUBDIR:-}"
imgsubdir="${imgsubdir#/}"

[ -d "$chroot" ] ||
	fatal "$dir: not look like a work directory of hasher."

[ -z "$pkgs" ] ||
	mki-install $pkgs ||
	fatal "failed to install packages: $pkgs."

[ -x "$handler" ] ||
	fatal "$handler: not executable."

message "$handler: executable file."

install -m755 -- "$handler" "$chroot/.host/script.sh"

run_chrooted "$chroot" <<-EOF
#!/bin/sh -efu
WORKDIR=/.image
OUTNAME='/.our/$outname'
SUBDIR='$imgsubdir'
EXCLUDE='${MKI_EXCLUDE-}'
export WORKDIR OUTNAME SUBDIR EXCLUDE
/.host/script.sh
EOF

mki-copy-our2out
